const path = require('path');
const HttpWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const Webpack = require('webpack');

let WEBPACK_ENV = process.env.WEBPACK_ENV || 'dev';
module.exports = {
  entry: path.join(__dirname, 'src', 'app.jsx'),
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'app.js',
    publicPath: WEBPACK_ENV === 'dev' ? '/dist/' : '//s.zhanghanyi.com/Try_Webpack_React/dist/'
  },
  // 用户路径起别名
  resolve: {
    alias: {
      page: path.resolve(__dirname, 'src/page/'),
      component: path.resolve(__dirname, 'src/component/'),
      util: path.resolve(__dirname, 'src/util/'),
      service: path.resolve(__dirname, 'src/service'),
    }
  },
  module: {
    rules: [
      {
        test: /\.jsx$/,
        exclude: /(node_modules)/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['env', 'react']
          }
        }
      },
      {
        test: /\.css$/,
        use: ExtractTextPlugin.extract({
          fallback: 'style-loader',
          use: 'css-loader'
        })
      },
      {
        test: /\.styl(us)?$/,
        use: ExtractTextPlugin.extract({
          fallback: 'style-loader',
          use: ['css-loader', 'stylus-loader']
        })
      },
      {
        test: /\.(png|jpg|gif)$/,
        use: [
          {
            loader: 'url-loader',
            options: {
              limit: 20000,
              name: 'assets/images/[name].[ext]',
            }
          }
        ]
      },
      {
        test: /\.(woff|woff2|eot|ttf|otf|svg)$/,
        use: [
          {
            loader: 'url-loader',
            options: {
              limit: 8192,
              name: 'assets/fonts/[name].[ext]'
            }
          }
        ]
      }
    ]
  },
  plugins: [
    new CleanWebpackPlugin(['dist']),
    new HttpWebpackPlugin({
      template: './index.html',
      favicon: './logo.png',
    }),
    // 独立css文件
    new ExtractTextPlugin('css/index.css'),
    // 提出公共模块
    new Webpack.optimize.CommonsChunkPlugin({
      name: 'common',
      filename: 'js/base.js'
    }),
  ],
  devServer: {
    port: 8086,
    contentBase: path.join(__dirname, "dist"),
    compress: true,
    historyApiFallback: { // 当为404时访问这个
      index: '/dist/'
    },
    proxy: {
      '/manage': {
        target: 'http://admintest.happymmall.com',
        changeOrigin: true,
      },
      '/user/logout.do': {
        target: 'http://admintest.happymmall.com',
        changeOrigin: true,
      }
    }
  }
};